package com.example.musicplayer.activity;


import android.content.ComponentName;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.musicplayer.MainActivity;
import com.example.musicplayer.R;
import com.example.musicplayer.base.BaseActivity;
import com.example.musicplayer.playtask.MusicPlayTask;
import com.example.musicplayer.pojo.Music;
import com.example.musicplayer.service.MusicService;

import java.io.Serializable;
import java.util.List;

public class MusicPlayerActivity extends BaseActivity implements View.OnClickListener {


    private TextView tvPlayName;
    private ImageView ivUpMusic;
    private ImageView ivPlayStartPause;
    private ImageView ivNextMusic;
    private List<Music> musicList;

    private MusicService.MusicPlayBinder musicPlayBinder;

    private ServiceConnection connection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            musicPlayBinder = (MusicService.MusicPlayBinder) service;
        }

        @Override
        public void onServiceDisconnected(ComponentName componentName) {

        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.music_player_layout);

        tvPlayName = findViewById(R.id.tv_play_name);
        ivUpMusic = findViewById(R.id.iv_up);
        ivPlayStartPause = findViewById(R.id.iv_play_start_pause);
        ivNextMusic = findViewById(R.id.iv_next_music);

        ivUpMusic.setOnClickListener(this);
        ivPlayStartPause.setOnClickListener(this);
        ivNextMusic.setOnClickListener(this);

        Intent intent = new Intent(this, MusicService.class);
        startService(intent); // 启动服务
        bindService(intent, connection, BIND_AUTO_CREATE); // 绑定服务

        musicList = (List<Music>) getIntent().getSerializableExtra("musicList");
        int musicPosition = getIntent().getIntExtra("MusicPosition", -1);
        // 设置播放界面正在播放音乐的歌名
        if (musicPosition == -1) {
            tvPlayName.setText("您还没有播放音乐");
        } else {
            Music music = musicList.get(musicPosition);
            tvPlayName.setText(music.getMusicName());
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction("com.example.action.PLAY_AND_PAUSE_ACTION");
        MusicPlayTask.MyTaskReciver myTaskReciver = new MusicPlayTask.MyTaskReciver();
        registerReceiver(myTaskReciver,intentFilter);
    }

    /**
     * 点击事件
     * @param v
     */
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
                // 上一曲
            case R.id.iv_up:

                break;
                // 播放和暂停
            case R.id.iv_play_start_pause:
//                musicPlayBinder.startMusic(musicList.get(0).getMusicPath());
                Intent sendTaskIntent = new Intent("com.example.action.PLAY_AND_PAUSE_ACTION");

                sendBroadcast(sendTaskIntent);
                break;
                // 下一曲
            case R.id.iv_next_music:

                break;
            default:
                break;
        }
    }
}